<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async (t) => {
  // Wait for after the load event so that the navigation doesn't get converted
  // into a replace navigation.
  await new Promise(r => window.onload = () => t.step_timeout(r, 0));
  let start_length = navigation.entries().length;
  let start_index = navigation.currentEntry.index;

  location.hash = "#1";
  location.hash = "#2";
  location.hash = "#3";

  assert_equals(navigation.entries().length, start_length + 3);
  const [entry0, entry1, entry2, entry3] = navigation.entries().slice(start_index);
  assert_equals((new URL(entry2.url)).hash, "#2");
  assert_equals((new URL(entry3.url)).hash, "#3");

  let dispose2Called = false;
  entry2.ondispose = t.step_func(e => {
    dispose2Called = true;

    assert_equals(e.constructor, Event);
    assert_equals(e.bubbles, false);
    assert_equals(e.cancelable, false);
    assert_equals(e.composed, false);

    assert_array_equals(
      navigation.entries().slice(start_index),
      [entry0, entry1, navigation.currentEntry],
      "entries() is updated during dispose for entry 2");
    assert_not_equals(navigation.currentEntry, entry1, "current entry must be updated during dispose for entry 3");
    assert_true(navigation.canGoBack, "canGoBack is still true during dispose for entry 2");
    assert_false(navigation.canGoForward, "canGoForward is still false during beforedispose for entry 2");
    assert_equals(navigation.transition.navigationType, "push", "transition navigationType during dispose for entry 2");
    assert_equals(navigation.transition.from, entry1, "transition from during dispose for entry 2");
    assert_equals(location.hash, "#fork", "location.hash is updated during dispose for entry 2");
  });

  entry3.addEventListener("dispose", t.step_func_done(e => {
    assert_true(dispose2Called, "dispose for entry 2 must have happened before entry 3");

    assert_array_equals(
      navigation.entries().slice(start_index),
      [entry0, entry1, navigation.currentEntry],
      "entries() is updated during dispose for entry 3");
    assert_not_equals(navigation.currentEntry, entry1, "current entry must be updated during dispose for entry 3");
    assert_true(navigation.canGoBack, "canGoBack is still true during dispose for entry 3");
    assert_false(navigation.canGoForward, "canGoForward is still false during beforedispose for entry 3");
    assert_equals(navigation.transition.navigationType, "push", "transition navigationType during dispose for entry 3");
    assert_equals(navigation.transition.from, entry1, "transition from during dispose for entry 3");
    assert_equals(location.hash, "#fork", "location.hash is updated during dispose for entry 3");
  }));

  await navigation.traverseTo(entry1.key).committed;

  navigation.addEventListener("navigate", e => {
    e.intercept();
  });

  navigation.navigate("#fork");

  assert_equals(navigation.entries().length, start_length + 2);
  const [finalEntry0, finalEntry1, finalEntry2] = navigation.entries().slice(start_index);
  assert_equals(finalEntry0, entry0);
  assert_equals(finalEntry1, entry1);
  assert_not_equals(finalEntry2, entry2);
  assert_equals(navigation.currentEntry, finalEntry2);
  assert_equals((new URL(finalEntry2.url)).hash, "#fork");
}, "dispose events when forward-pruning same-document entries");
</script>
